package ee.rumba.iPhoneShop.artwork;

import java.io.File;
import java.io.FileInputStream;

public class UIKitParser {
  /*
   version 1.0.2   (UIKit size=2189124)
    _ImageMapCoords_Other                    0x1366fc  332
    _ImageMapCoords_Keyboard_Common          0x137164  11
    _ImageMapCoords_Keyboard_StandardQWERTY  0x1371bc  107

    version 1.1.1  (UIKit size=2627996)
    _ImageMapCoords_Other                   0x142ac0   341
    _ImageMapCoords_Keyboard_Japanese       0x143568   46
    _ImageMapCoords_Keyboard_Latin          0x1436d8   102
    _ImageMapCoords_Keyboard_Common         0x143a08   26

   */
  byte[] fileContent;
  public UIKitParser(File uikitFile) throws Exception {
    fileContent = new byte[(int)uikitFile.length()];
    FileInputStream fin = new FileInputStream(uikitFile);
    int read = fin.read(fileContent);
    System.out.println("read: " + read);
    printJavaCode(fileContent);
  }

  private void printJavaCode(byte[] fileContent) {
    if (fileContent.length == 2189124) { // version1.0.2
      printBlockJavaCode("Other", "102", 0x1366fc, 332, fileContent);
      printBlockJavaCode("Keyboard_Common", "102", 0x137164, 11, fileContent);
      printBlockJavaCode("Keyboard_StandardQWERTY", "102", 0x1371bc, 107, fileContent);

    } else if (fileContent.length == 2627996) { // version 1.1.1
      printBlockJavaCode("Other", "111", 0x142ac0, 341, fileContent);
      printBlockJavaCode("Keyboard_Japanese", "111", 0x143568, 46, fileContent);
      printBlockJavaCode("Keyboard_Latin", "111", 0x1436d8, 102, fileContent);
      printBlockJavaCode("Keyboard_Common", "111", 0x143a08, 26, fileContent);
    }
  }

  private void printBlockJavaCode(String file, String version, int start, int count, byte[] array) {
    System.out.println("  private void init" + file + "_ver" + version + "() {");
    System.out.println("    artworkItems = new Vector<ArtworkItem>(" + count + ");");

    for (int i=0; i<count; i++) {
      int xpos = start + i*8;
      byte bit = array[xpos];
      int x = readShort(array, xpos+4);
      int y = readShort(array, xpos+6);
      int bstart = readInt(array, xpos);
      int bsize = x*y*4;
      System.out.println("    artworkItems.add(new ArtworkItem(" + x + ", " + y + ", 0x" + Integer.toHexString(bstart) + ", \"" + file + "-ver" + version + "_" + i + ".png\"));");
    }
    System.out.println("  }");
  }
  private void analyse(byte[] fileContent) {
    int xstart = 0x143a08;
    for (int pos = 0; pos < 500; pos++) {
      printParams(fileContent, xstart, pos);
    }
  }
  
  private void printParams(byte[] array, int start, int pos) {
    int xpos = start + pos*8;
    System.out.print(pos+1 + " ");
    System.out.print(Integer.toHexString(xpos));
    System.out.print(" " + Integer.toHexString(readInt(array, xpos)));
    System.out.print(" " + readShort(array, xpos+4));
    System.out.println(" x " + readShort(array, xpos+6));
  }

  private int readInt(byte[] array, int pos) {
    int ret = (array[pos+1] & 0xFF) << 8;
    ret |= (array[pos+2] & 0xFF) << 16;
    ret |= (array[pos+3] & 0xFF) << 24;
    return ret;
  }

  private int readShort(byte[] array, int pos) {
    int ret = array[pos] & 0xFF;
    ret |= (array[pos+1] & 0xFF) << 8;

    return ret;
  }

  public static void main(String args[]) throws Exception {
    File uikitFile = new File(args[0]);
    if (uikitFile.exists()) {
      UIKitParser uikp = new UIKitParser(uikitFile);      
    } else {
      System.out.println("File not found");
    }
  }


}
